package com.demo.web;

import com.demo.common.Constants;
import com.demo.dto.BaseResult;
import com.demo.dto.ListResult;
import com.demo.dto.Pagination;
import com.demo.entity.SaleCashApply;
import com.demo.entity.User;
import com.demo.service.ISaleCashApplyService;
import com.demo.service.ISalePersonService;
import com.demo.vo.SaleCashApplyVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Transactional
@Controller
@RequestMapping("/admin/saleCashApply")
public class SaleCashApplyController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ISaleCashApplyService service;
    @Autowired
    private ISalePersonService salePersonService;

    /**
     * 创建
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public BaseResult<SaleCashApply> get(SaleCashApply arg) {
        SaleCashApply service_res = service.selectByPrimaryKey(arg.getId());
        BaseResult<SaleCashApply> res = new BaseResult<SaleCashApply>(true, service_res);
        return res;
    }


    /***
     * 修改数据
     * @param arg
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/check", method = RequestMethod.POST)
    public BaseResult<Object> update(SaleCashApply arg, HttpSession session) {
        User user = (User) session.getAttribute(Constants.USER);
        SaleCashApply sca = new SaleCashApply();
        sca.setId(arg.getId());
        sca.setStatus(arg.getStatus());
        sca.setRefuseReason(arg.getRefuseReason());
        BaseResult<Object> res;
        //接受/拒绝
        if ("1".equals(sca.getStatus()) || "2".equals(sca.getStatus())) {
            if (user != null) {
                Object service_res = service.check(sca);
                if (service_res instanceof String) {
                    res = new BaseResult(false, (String) service_res, 403);
                } else {
                    res = new BaseResult(true, (Integer) service_res);
                }
            } else {
                res = new BaseResult(false, "请登录!", 401);
            }
        } else {
            res = new BaseResult(false, "无权操作!", 403);
        }
        return res;
    }


    @ResponseBody
    @RequestMapping(value = "/pay", method = RequestMethod.POST)
    public BaseResult<Object> pay(SaleCashApply arg, HttpSession session) {
        User user = (User) session.getAttribute(Constants.USER);
        SaleCashApply sca = new SaleCashApply();
        sca.setId(arg.getId());
        sca.setStatus(arg.getStatus());
        BaseResult<Object> res;
        //接受/拒绝
        if ("3".equals(sca.getStatus())) {
            if (user != null) {
                Object service_res = service.pay(sca);
                if (service_res instanceof String) {
                    res = new BaseResult(false, (String) service_res, 403);
                } else {
                    res = new BaseResult(true, (Integer) service_res);
                }
            } else {
                res = new BaseResult(false, "请登录!", 401);
            }
        } else {
            res = new BaseResult(false, "无权操作!", 403);
        }
        return res;
    }

    /***
     * 分页查询
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ListResult<SaleCashApplyVo> query(SaleCashApplyVo arg) {
        List<SaleCashApplyVo> list = service.queryByPage(arg);
        int total = service.queryCountByPage(arg);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        ListResult<SaleCashApplyVo> res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }
}
